// pages/center/center.js
const { out,newname } = require("../../utils/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:"",
    show:false,
    nickName:''
  },
  newtouxiang(){
    let that=this
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res) {
        console.log(res)
        that.setData({
          imglist:res.tempFiles[0]
        })
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'avatarfile',
          url: 'http://192.168.1.103:8080/program/avatar',
          header:{'Authorization': wx.getStorageSync('token')},
          formData:{'content-type':'application/x-www-form-urlencoded'},
          success (res){
            console.log(res)
            let jsonS = JSON.parse(res.data)
            console.log(jsonS)
            wx.setStorageSync('avatar', jsonS.imgUrl)
            that.setData({
              avatarUrl:jsonS.imgUrl

            })
          }
        })
      }
  })
  },
  newname(){
    let p={
      nickName:this.data.nickName
    }
    newname(p).then(res=>{
      console.log(res)
      if(res.code==200){
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
        wx.setStorageSync('nickName',this.data.nickName)
        this.setData({
          show:false
        })
      }
    })
  },
  inputnickname(e){
    this.setData({
      nickName:e.detail.value
    })
  },
  putname(){
    this.setData({
      show:true
    })
  },
  onChooseAvatar(e){
    this.setData({
      avatarUrl:e.detail.avatarUrl
    })
    console.log(e)
  },
  nicheng(){
    wx.navigateTo({
      url: '/pages/nicheng/nicheng',
    })
  },
  loginout(){
    wx.showModal({
      title: '提示',
      content: '确认退出当前账户',
      success (res) {
        if (res.confirm) {
        let p={
          token:wx.getStorageSync('token')
        }
      out(p).then(res=>{
           if(res.code==200){
            wx.showToast({
              title: '已退出',
              icon: 'none',
              duration: 2000
            })
            wx.clearStorageSync()
            wx.reLaunch({
              url: '/pages/login/login',
            })
           
           }
       } )
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  remind(){
    wx.navigateTo({
      url: '/pages/remind/remind',
    })
  },
  password(){
    wx.navigateTo({
      url: '/pages/password/password',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   this.setData({
    nickName:wx.getStorageSync('nickName'),
    avatarUrl:wx.getStorageSync('avatar')
   })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})